package com.itboye.mengwu.app.ui.mymeng.personsettings;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.LocationAdapter;
import com.itboye.mengwu.data.model.AddressQueryModel;
import com.itboye.mengwu.data.model.PersonDataBean;
import com.itboye.mengwu.data.presenter.AddressQueryPresenter;

public class AddressManageActivity extends BaseActivity implements
		OnClickListener, Observer {

	TextView btn_addnewaddr, txt_title;
	ImageView img_other;
	ListView listView;
	AddressQueryPresenter addressQueryPresenter;
	ArrayList<AddressQueryModel> arrayListAddress;
	ArrayList<AddressQueryModel> arrayListAddressCopy;
	private LocationAdapter adapter;
	RelativeLayout noData;
	private View footerView;
	private int operate = 101;
	private String uid;
	private String defaultAddress;
	int type;
	ImageView back;

	RelativeLayout ralinyoutaddress;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_address_manage;
	}

	@Override
	public void onMyCreate() {
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		type = getIntent().getIntExtra("type", 0);
		arrayListAddress = new ArrayList<AddressQueryModel>();
		arrayListAddressCopy = new ArrayList<AddressQueryModel>();
		footerView = LayoutInflater.from(this).inflate(
				R.layout.item_location_listview_footer, null);
		listView.addFooterView(footerView);
		addressQueryPresenter = new AddressQueryPresenter(this);
		addressQueryPresenter.queryAddress(uid);
		showProgressDialog("数据加载中...", true);
		img_other.setVisibility(View.GONE);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		footerView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(AddressManageActivity.this,
						AddAddressActivity.class);
				startActivityForResult(intent, operate);// (intent);
			}
		});
		if (type == 0)
			txt_title.setText("所有地址");
		else if (type == 1)
			txt_title.setText("选择地址");
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// TODO Auto-generated method stub
				Intent intent = new Intent();

				String province = arrayListAddressCopy.get(position)
						.getProvince();
				String city = arrayListAddressCopy.get(position).getCity();
				String area = arrayListAddressCopy.get(position).getArea();
				if (city.equals("市辖区") | city.equals("县")) {
					city = "";
				}
				if (area.equals("市辖区")) {
					area = "";
				}
				String wholelocation = province + city + area;
				String detail = arrayListAddressCopy.get(position)
						.getDetailinfo();
				String zipcode = arrayListAddressCopy.get(position)
						.getPostal_code();
				String name = arrayListAddressCopy.get(position)
						.getContactname();
				String phone = arrayListAddressCopy.get(position).getMobile();
				String locationid = arrayListAddressCopy.get(position).getId();
				intent.putExtra("id", locationid);
				intent.putExtra("wholelocation", wholelocation);
				intent.putExtra("detail", detail);
				intent.putExtra("zipcode", zipcode);
				intent.putExtra("name", name);
				intent.putExtra("phone", phone);
				intent.putExtra("isDefault", arrayListAddressCopy.get(position)
						.getDefault());
				if (type == 0) {
					intent.setClass(AddressManageActivity.this,
							DetailLocationActivity.class);
					startActivityForResult(intent, operate);// (intent);
				} else {
					setResult(102, intent);
					finish();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_addnewaddr:

			Intent intent = new Intent(this, AddAddressActivity.class);
			startActivityForResult(intent, operate);
			break;
		case R.id.back:
			finish();
		default:
			break;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		System.out.println(entity.getData());
		if (entity != null) {
			if (entity.getEventType() == AddressQueryPresenter.AddressQuerySuccessEvent) {
				noData.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				arrayListAddress.clear();
				arrayListAddressCopy.clear();
				arrayListAddress.addAll((ArrayList<AddressQueryModel>) entity
						.getData());
				for (int i = 0; i < arrayListAddress.size(); i++) {
					if (arrayListAddress.get(i).getDefault() == 1) {
						arrayListAddressCopy.add(0, arrayListAddress.get(i));
					} else {
						arrayListAddressCopy.add(arrayListAddress.get(i));
					}
				}
				adapter = new LocationAdapter(this, arrayListAddressCopy);
				if (adapter == null) {
					ralinyoutaddress.setVisibility(View.GONE);
				} else if (adapter != null) {
					ralinyoutaddress.setVisibility(View.VISIBLE);
					listView.setAdapter(adapter);
				}

			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		addressQueryPresenter = new AddressQueryPresenter(this);
		addressQueryPresenter.queryAddress(uid);
		// addressQueryPresenter.getDefaultAddr(uid);
	}

}
